<h2>Service Invoice details</h2>
<?php
    $this->dojoLayer('common');
    $this->headTitle('Service Invoice details');
    $invoiceId = $this->escape($this->invoiceId);
    $acl = $this->acl;
?>

<?php if ($acl->isAllowed($this->currentUser,'access invoice pages')): ?>   
<a href="<?php echo $this->url(
    array(
        'module' => 'default',
        'controller' => 'invoice',
        'action' => 'index',
    ), 'default', true
);?>">Invoices</a>
| 
<?php endif; ?>

<?php if ($acl->isAllowed($this->currentUser,'edit invoices')): ?>
<a href="<?php echo $this->url(
    array(
        'module' => 'default',
        'controller' => 'invoice',
        'action' => 'editserviceinvoice',
        'invoice_id' => $invoiceId,
    ), 'default', true
);?>">Edit</a>
|
<?php endif; ?>

<?php if  ($acl->isAllowed($this->currentUser, 'delete invoices')): 
    $deleteUrl = $this->url(
                    array(
                        'module' => 'default',
                        'controller' => 'invoice',
                        'action' => 'delete',
                        'invoice_id' =>$this->escape($invoiceId)
                    )
                );
                echo $this->deleteButton($deleteUrl); 
 endif; ?>
|

<?php if ($acl->isAllowed($this->currentUser,'access invoice pages')): ?>
<a href="<?php echo $this->url(
    array(
        'module' => 'default',
        'controller' => 'invoice',
        'action' => 'exportservicepdf',
        'invoice_id' => $invoiceId,
    ), 'default', true
);?>">Export to pdf</a>
<?php endif; ?>

<?php
    $invoiceData = $this->invoiceData;
?>
<table class="data_table">
    <tr>
        <td>Invoice Id</td><td><?php echo $this->escape($this->invoiceId); ?></td>
    </tr>
    <tr>
        <td>Invoice Type</td><td> Service</td>
    </tr>
    <tr>
        <td>Total amount</td>
        <td><?php echo $this->escape($this->invoiceTotal($this->invoiceId)); ?></td>
    </tr>
    <tr>
        <td>Party type</td>
        <td>
            <?php 
                if ($invoiceData['to_type'] == 
                    Core_Model_Invoice::TO_TYPE_ACCOUNT) {
                    echo 'Account';
                } else {
                    echo "Contact";
                }
                echo " <br />";
                echo $this->partyHyperlink($invoiceData['to_type'], 
                                                    $invoiceData['to_type_id']);
            ?>
        </td>
    </tr>
    <tr>
        <td>Invoice to</td>
        <td><?php echo $this->invoiceParty($invoiceData['to_type'], 
                                            $invoiceData['to_type_id']); ?></td>
    </tr>
    <tr>
        <td>Campaign id </td>
        <td><?php  if ($invoiceData['campaign_id']) {
                        echo $this->campaignName($this->escape(
                                                $invoiceData['campaign_id']));
             }   ?> </td>
    </tr>
    <tr>
        <td>Created on</td>
        <td><?php echo $this->timestampToHuman($this->escape(
                                            $invoiceData['created'])); ?></td>
    </tr>
    <tr>
        <td>Created by</td>
        <td><?php echo $this->userHyperlink($this->escape(
                     $invoiceData['created_by'])); ?></td>
    </tr>
    <tr>
        <td>Branch</td>
        <td><?php echo $this->branchHyperlink($this->escape(
                    $invoiceData['branch_id'])); ?></td>
    </tr>
    <tr>
        <td>Delivery Terms</td>
        <td><?php echo $this->escape($invoiceData['delivery_terms']); ?></td>
    </tr> 
    <tr>
        <td>Payment Terms</td>
        <td><?php echo $this->escape($invoiceData['payment_terms']); ?></td>
    </tr> 
    <tr>
        <td>Invoice Notes</td>
        <td><?php echo $this->escape($invoiceData['notes']); ?></td>
    </tr>  
    
</table>


<table class="data_table">
    <tr>
        <th class="tinyshort">Serial #</th>
        <th>Item</th>
        <th>Price</th>
        <th>Tax Type</th>
        <th>Tax</th>
        <th>Total</th>
    </tr>
    <?php
        $serialNumber = 1;
        foreach ($this->invoiceItems as $item) {
            echo "<tr>";
            echo sprintf("<td>%d</td>", $serialNumber++);
            echo "<td>";
            echo $this->escape($this->serviceItemName(
                     $item['service_item_id']
                ));
            echo "<br />";
            echo $this->escape($item['description']);
            echo "</td>";
            $unitPrice = $item['amount'];
            echo "<td>" . number_format($unitPrice, 2, '.', ',') . "</td>";
            
            echo "<td>" . $this->taxNameById($item['tax_type_id']) . "</td>";
            $taxPercentage = $this->taxPercentageById($item['tax_type_id']);
            $beforeTax = $unitPrice * 1;
            $tax = ($beforeTax * $taxPercentage) / 100;
            echo "<td>" . $tax . "</td>";
            $lineTotal = $beforeTax + $tax;
            echo "<td>" . number_format($lineTotal, 2, '.', ',') . "</td>";
            echo "</tr>";
        }
    ?>
</table>
